unit Unit13;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, db, FileUtil, Forms, Controls, Graphics, Dialogs, Buttons,
  StdCtrls, DBGrids, IBQuery, IBDatabase, ZDataset;

type

  { TForm13 }

  TForm13 = class(TForm)
    bt_ok: TBitBtn;
    bt_anuluj: TBitBtn;
    bt_dodaj: TButton;
    bt_edytuj: TButton;
    bt_drukuj: TButton;
    bt_kasuj: TButton;
    bt_zamknij: TButton;
    bt_historia: TButton;
    Datasource1: TDatasource;
    DBGrid1: TDBGrid;
    Edit1: TEdit;
    ibq1: TIBQuery;
    ibq2: TIBQuery;
    ibt1: TIBTransaction;
    ibt2: TIBTransaction;
    Label1: TLabel;
    procedure bt_dodajClick(Sender: TObject);
    procedure bt_edytujClick(Sender: TObject);
    procedure bt_kasujClick(Sender: TObject);
    procedure bt_zamknijClick(Sender: TObject);
    procedure DBGrid1EditingDone(Sender: TObject);
    procedure DBGrid1Enter(Sender: TObject);
    procedure DBGrid1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure Edit1Change(Sender: TObject);
    procedure FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form13: TForm13;

implementation
uses
  unit14, unit5, unit19;
{$R *.lfm}

{ TForm13 }

// nowa kartoteka
procedure TForm13.bt_dodajClick(Sender: TObject);
begin
   form14.Edit1.Clear;
   form14.Edit2.Clear;
   form14.ActiveControl:=form14.Edit1;
   form14.Caption:='Nowa kartoteka';
   FillStrings(ibq2, 'asort_grup', form14.ComboBox1.Items);
   if form14.ShowModal = mrOK then
   begin
     if ibq1.Active then ibq1.Close;
     ibq1.SQL.Clear;
     ibq1.SQL.Add('insert into kartoteki (ean, nazwadl, data_zal, stan) values (:a,:b,:c,:d)');
     ibq1.ParamByName('a').AsString:=form14.Edit1.Text;
     ibq1.ParamByName('b').AsString:=form14.Edit2.Text;
     ibq1.ParamByName('c').AsDateTime:=Now();
     ibq1.ParamByName('d').AsFloat:=0.00;
     try
       ibq1.Transaction.StartTransaction;
       ibq1.ExecSQL;
       ibq1.Transaction.Commit;
     except
       ibq1.Transaction.Rollback;
     end;
     FormShow(nil);
   end;
end;

// edycja
procedure TForm13.bt_edytujClick(Sender: TObject);
var
  key : integer;
begin
  key:=ibq1.FieldByName('kartoteki_idx').AsInteger;
  form14.Caption:='Edycja kartoteki';
  form14.Edit1.Text:=ibq1.FieldByName('ean').AsString;
  form14.Edit2.Text:=ibq1.FieldByName('Asortyment').AsString;
  form14.ActiveControl:=form14.Edit1;
  if form14.ShowModal = mrOK then
  begin
    if ibq1.Active then ibq1.Close;
    ibq1.SQL.Clear;
    ibq1.SQL.Add('update kartoteki set nazwadl = :a, ean = :b where kartoteki_idx = :c');
    ibq1.ParamByName('a').AsString:=form14.Edit2.Text;
    ibq1.ParamByName('b').AsString:=form14.Edit1.Text;
    ibq1.ParamByName('c').AsInteger:=key;
    try
      ibq1.Transaction.StartTransaction;
      ibq1.ExecSQL;
      ibq1.Transaction.Commit;
    except
      ibq1.Transaction.Rollback;
    end;
    FormShow(nil);
  end;
end;

procedure TForm13.bt_kasujClick(Sender: TObject);
var
  key : integer;
begin
  key:=ibq1.FieldByName('kartoteki_idx').AsInteger;
  if form5.ShowModal = mrOK then
  begin
    if ibq1.Active then ibq1.Close;
    ibq1.SQL.Clear;
    ibq1.SQL.Add('delete from kartoteki where kartoteki_idx = :a');
    ibq1.ParamByName('a').AsInteger:=key;
    try
      ibq1.Transaction.StartTransaction;
      ibq1.ExecSQL;
      ibq1.Transaction.Commit;
    except
      ibq1.Transaction.Rollback;
    end;
    FormShow(nil);
  end;
end;

procedure TForm13.bt_zamknijClick(Sender: TObject);
begin
  close;
end;

procedure TForm13.DBGrid1EditingDone(Sender: TObject);
begin

end;

procedure TForm13.DBGrid1Enter(Sender: TObject);
begin

end;

procedure TForm13.DBGrid1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
end;

procedure TForm13.Edit1Change(Sender: TObject);
begin
  FormShow(nil);
end;

procedure TForm13.FormKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
  if key = 13 then
  begin
    ModalResult:=mrOK;
    hide;
  end;
end;

procedure TForm13.FormShow(Sender: TObject);
begin
  ActiveControl:=edit1;
  if ibt1.Active then ibt1.Commit;
  ibq1.SQL.Clear;
  if trim(edit1.Text) <> '' then
  begin
    ibq1.SQL.Add('select k.nazwadl "Asortyment" ,k.ean,k.indeks,k.stan,k.data_zal,g.nazwadl "Grupa",k.grup_idx,k.kartoteki_idx');
    ibq1.SQL.Add('from kartoteki k');
    ibq1.SQL.Add('left join asort_grup g on k.grup_idx = g.asort_grup_idx');
    ibq1.SQL.Add('where (upper(k.nazwadl) like :a) or (ean like :a) or (indeks like :a) order by upper(k.nazwadl)');
    ibq1.ParamByName('a').AsString:='%'+UpperCase(edit1.Text)+'%';
//    ibq1.ParamByName('b').AsString:='%'+UpperCase(edit1.Text)+'%';
  end
  else
    begin
      ibq1.SQL.Add('select k.nazwadl "Asortyment" ,k.ean,k.indeks,k.stan,k.data_zal,g.nazwadl "Grupa",k.grup_idx,k.kartoteki_idx');
      ibq1.SQL.Add('from kartoteki k');
      ibq1.SQL.Add('left join asort_grup g on k.grup_idx = g.asort_grup_idx');
      ibq1.SQL.Add('order by upper(k.nazwadl)');
    end;
  try
    ibq1.Transaction.StartTransaction;
    ibq1.Open;
  except
    ibq1.Transaction.Rollback;
  end;
end;

end.

